Python syntax and semantics
part 19/45 · 74.8 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Python has arbitrary-length integers and automatically increases their storage size as necessary. Prior to Python 3, there were two kinds of integral numbers: traditional fixed size integers and "long" integers of arbitrary size. The conversion to "long" integers was performed automatically when required, and thus the programmer usually did not have to be aware of the two integral types. In newer language versions the distinction is completely gone and all integers behave like arbitrary-length integers.
Python supports normal floating point numbers, which are created when a dot is used in a literal (e.g. 1.1), when an integer and a floating point number are used in an expression, or as a result of some mathematical operations ("true division" via the / operator, or exponentiation with a negative exponent).
Python also supports complex numbers natively. The imaginary component of a complex number is indicated with the J or j suffix, e.g. 3 + 4j.
Lists, tuples, sets, dictionaries
Python has syntactic support for the creation of container types.
Lists (class list) are mutable sequences of items of arbitrary types, and can be created either with the special syntax
a_list = [1, 2, 3, "a dog"]
or using normal object creation
a_second_list = []
a_second_list.append(4)
a_second_list.append(5)
Tuples (class tuple) are immutable sequences of items of arbitrary types. There is also a special syntax to create tuples
a_tuple = 1, 2, 3, "four"
a_tuple = (1, 2, 3, "four")
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────